import requests

"""
封装http请求  暂时：post get请求
"""


class HttpBase:
    def __init__(self, url, data, method, headers):
        self.url = url
        self.data = data
        self.method = method
        self.headers = headers

    def getHttpBody(self):
        method = {
            'get': requests.get(self.url, params=self.data, headers=self.headers),
            'post': requests.post(self.url, json=self.data, headers=self.headers),
            'Get': requests.get(self.url, params=self.data, headers=self.headers),
            'Post': requests.post(self.url, json=self.data, headers=self.headers),
            'postForm': requests.post(self.url, data=self.data, headers=self.headers),
            'put': requests.put(self.url, data=self.data, headers=self.headers)
        }.get(self.method, requests.get(self.url, params=self.data, headers=self.headers))
        result = method.content
        return result

    def RequestRun(self):
        method = {
            'get': requests.get(self.url, params=self.data, headers=self.headers),
            'post': requests.post(self.url, json=self.data, headers=self.headers),
            'Get': requests.get(self.url, params=self.data, headers=self.headers),
            'Post': requests.post(self.url, json=self.data, headers=self.headers),
            'postForm': requests.post(self.url, data=self.data, headers=self.headers),
            'put': requests.put(self.url, data=self.data, headers=self.headers)
        }.get(self.method, requests.get(self.url, params=self.data, headers=self.headers))
        # print(method.content)
        return method

    def getHttpHeaders(self):
        method = {
            'get': requests.get(self.url, params=self.data, headers=self.headers),
            'post': requests.post(self.url, json=self.data, headers=self.headers),
            'Get': requests.get(self.url, params=self.data, headers=self.headers),
            'Post': requests.post(self.url, json=self.data, headers=self.headers),
            'postForm': requests.post(self.url, data=self.data, headers=self.headers),
            'put': requests.put(self.url, data=self.data, headers=self.headers)
        }.get(self.method, requests.get(self.url, params=self.data, headers=self.headers))
        result = method.headers
        return result


if __name__ == '__main__':
    url = "http://192.168.17.236:30510/upms-provider/tenant/user/login/mobile"
    data = {'mobile': '18368093710', 'password': 'Aa123456'}
    headers = {
        "Content-Type": "application/json"
    }
    cookie = HttpBase(url, data, "Post", headers).getHttpBody()
    cookie1 = requests.post(url, json=data, headers=headers)
    print(str(cookie, 'utf-8'))
    print(str(cookie1.content, 'utf-8'))
